<extend name="Public:base"/>

<block name="main">
<div id="main-box">

    <div class="main-title">
        <h2>添加字段</h2>
    </div>

    <form name="myform" class="form-horizontal" id="myform" action="{:U('Field/insert')}" method="post">
        <input type="hidden" name="moduleid" value="{$moduleid}"/>
        <div class="form-group">
            <label class="control-label col-md-2">*字段类型</label>
            <div class="col-md-5">
            <select id="type" name="type" class="required form-control" id="type" minlength="1" onchange="javascript:field_setting(this.value);" >
                <option value='' >请选择字段类型</option>
                <option value="catid">栏目</option>
                <option value="title">标题</option>
                <option value="typeid">类别</option>
                <option value="text">单行文本</option>
                <option value="textarea">多行文本</option>
                <option value="editor">编辑器</option>
                <option value="select">下拉列表</option>
                <option value="radio">单选按钮</option>
                <option value="checkbox">复选框</option>
                <option value="image">单张图片</option>
                <option value="images">多张图片</option>
                <option value="file">单文件上传</option>
                <option value="files">多文件上传</option>
                <option value="number">数字</option>
                <option value="datetime">日期和时间</option>
                <option value="posid">推荐位</option>
                <option value="guanlian">关联</option>
                <option value="groupid">会员组</option>
                <option value="linkage">联动菜单</option>
                <option value="template">模板选择</option>
                <option value="verify">验证码</option>
            </select>
            </div>
        </div>

        <div id="field_tr" class="form-group">
            <label class="control-label col-md-2"><font color="red">*</font>字段名</label>
            <div class="col-md-5">
                <input type="text" id="field" name="field" value="{$vo.field}" class="form-control"
                <if condition="$action_name=='add'"> validate="required:true, english:true,remote: '{:U($module_name.'/insert?isajax=1&moduleid='.$_GET['moduleid'])}' ,minlength:2, maxlength:20" </if>  />
            </div>
        </div>
        <div id="field_tr" class="form-group">
            <label class="control-label col-md-2"><font color="red">*</font>别名 </label>
            <div class="col-md-5">
                <input type="text" id="name" name="name" value="{$vo.name}" class="form-control required" minlength="2"  /> 
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-md-2">字段相关设置</label>
            <div id="field_setup" class="col-md-5">
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-md-2">字段class名称</label>
            <div class="col-md-5"><input type="text" id="class" name="class" value="{$vo.class}" size="10" class="form-control" /></div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-2">必填</label>
            <div class="col-md-5"><input type="radio" id="required" name="required" value="1"<if condition="$vo[required]==1">  checked</if>>是  <input type="radio" name="required" value="0" <if condition="$vo[required]==0">  checked</if>> 否
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-2">验证规则
            </label>
            <div class="col-md-5">{:Form::select(array('field'=>'pattern','options'=>$field_pattern),$vo[pattern])}
         </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-2">限制字符串长度范围</label>
            <div class="col-md-5">
                最小 <input type="text" id="minlength" name="minlength" value="" size="2" class="form-control" />
                最大 <input type="text" id="maxlength" name="maxlength" value="{$vo.maxlength}" class="form-control" size="2" />个字符
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-2">验证失败错误信息</label>
            <div class="col-md-5">
                <input type="text" id="errormsg" name="errormsg" value="" class="form-control"  size="50" />
                </div>
            </div>
        <div class="form-group">
            <label class="control-label col-md-2">在前台投稿中显示</label>
            <div class="col-md-5">
                {:Form::radio(array('field'=>'ispost','options'=>$options),$vo[ispost])}
                </div>
            </div>

        <div class="form-group">
            <label class="control-label col-md-2">禁止设置字段值的会员组</label>
            <div class="col-md-5">
                {:Form::checkbox(array('field'=>'unpostgroup','options'=>$usergroup,'setup'=>array('labelwidth'=>'90')),$vo[unpostgroup])}
                </div>
            </div>
        </table>
        <div class="form-actions">
            <input type="submit" value="提交" class="btn btn-sm btn-primary" />
            <input type="reset" value="取消" class="btn btn-sm btn-primary"/>
        </div>
    </form>

</div>

<script type="text/javascript">
$(document).ready(function() {
    $('#myform').ajaxForm({
        success:  complete,  // post-submit callback
        dataType: 'json'
    });
});

function complete(data){
    if(data.status==1){
        $.scojs_message('修改成功', $.scojs_message.TYPE_OK);
        setTimeout(function(){
            window.location.href = '{:U("Field/index")}'+'&moduleid={$moduleid}';
        },1000);
    }else{
        $.scojs_message(data.info, $.scojs_message.TYPE_ERROR);
    }

}
</script>
<script>
$('#nav ul a ').removeClass('on');
$('#nav ul').append('<a href="{:U("field/index",array(moduleid=>$_GET[moduleid]))}" class="edit">{:L(field_manage)}</a> | <a href="{:U("Field/add",array(moduleid=>$_GET[moduleid]))}" class="add">{:L(field_add)}</a> |');
<if condition="$action_name=='add'"> $('#nav ul a.add ').addClass('on');</if>
<if condition="$action_name=='edit'"> $('#nav ul a.edit ').addClass('on');</if>

$.ajaxSetup ({ cache: false });
function field_setting(type){
    if(type=='verify'){
        $('#field_tr').hide();
        $('#field').val('verifyCode');
        $('#name').val('验证码');
        $('#pattern').val('en_num');
        $('#minlength').val('4');
        $('#maxlength').val('4');

        $('#required').attr("checked",true);
        $('#ispost_1').attr("checked",true);

    }else{
        $('#field_tr').show();
    }
    var data =  {$vo["setup"]|json_encode};
    var url =  "{:U('field/add')}&isajax=1&moduleid={$moduleid}&type="+type;
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        beforeSend:function(){
            $('#field_setup').html('<img src="./Public/images/msg_loading.gif">');
        },
        success: function(msg){
            $('#field_setup').html(msg);
        },
        complete:function(){
        },
        error:function(){
        }
    });
}
</script>
<script>
$('#type').val('{$vo[type]}');
field_setting('{$vo[type]}');

</script>
</block>